package domain;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class provides all sort of utils, that can be reused and therefor don't belong in another class
 *
 * @author ParaSait
 */
public class Utils {

    /**
     * Checks if an email address is valid.
     *
     * @param email The email address you wan't to validate
     * @return True or false
     */
    public static boolean isValidEmail(String email) {
        Pattern pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * Computes the SHA-1 hash of a byte array
     *
     * @param array A byte array
     * @return String with SHA-1 hash of the byte array
     */
    public static String byteArrayToHex(byte[] array) {
        StringBuilder sb = new StringBuilder();
        for (byte b : array) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }

    /**
     * Computes the SHA-1 hash of a file
     *
     * @param file The file
     * @return The array of bytes for the resulting hash value.
     * @throws IOException If there is an error reading the file.
     */
    public static byte[] calculateFileHash(File file) throws IOException {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            FileInputStream stream = new FileInputStream(file);
            byte[] dataBytes = new byte[1024];
            int nread = 0;

            while ((nread = stream.read(dataBytes)) != -1) {
                md.update(dataBytes, 0, nread);
            }
            stream.close();

            byte[] mdbytes = md.digest();
            return mdbytes;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new IOException("Error reading file");
        }
        return null;
    }

    private static HashMap<String, File> getFileMap(HashMap<String, File> fileMap, File root, File subdir) {
        File[] fileArray = subdir.listFiles();
        for (File file : fileArray) {
            if (!file.isDirectory()) {
                fileMap.put(root.toURI().relativize(file.toURI()).getPath(), file);
            } else {
                getFileMap(fileMap, root, file);
            }
        }
        return fileMap;
    }

    /**
     * Returns a hashmap with: <br />
     * All the files (also in subdirectories) starting from the root param <br />
     * Key = The relative path of the file to the root param <br />
     * Value = The file
     *
     * @param root The directory from where you want to start
     * @return The hashmap with (relative path, file) pairs
     */
    public static HashMap<String, File> getFileMap(File root) {
        return getFileMap(new HashMap<String, File>(), root, root);
    }

}